package com.modelo.dao.proveedor;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;

import com.modelo.dao.comun.HibernateBaseDAO;
import com.modelo.entity.Proveedor;

@Repository
public class ProveedorDAO extends HibernateBaseDAO<Proveedor> implements IProveedorDAO,
		Serializable {

	public static final String CLAVE = "clave";
	public static final String CONTACTO = "contacto";

	public static final String DNI_CIF = "dniCif";
	public static final String EMAIL = "email";
	private static final Logger log = LoggerFactory.getLogger(ProveedorDAO.class);
	public static final String MOVIL = "movil";
	public static final String NOMBRE = "nombre";
	private static final long serialVersionUID = 1L;

	public static ProveedorDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ProveedorDAO) ctx.getBean("ProveedorDAO");
	}

	@Autowired
	public ProveedorDAO(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	public List<Proveedor> findByClave(Object clave) {
		return findByProperty(CLAVE, clave);
	}

	public List<Proveedor> findByContacto(Object contacto) {
		return findByProperty(CONTACTO, contacto);
	}

	public List<Proveedor> findByDniCif(Object dniCif) {
		return findByProperty(DNI_CIF, dniCif);
	}

	public List<Proveedor> findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List<Proveedor> findByExample(Proveedor instance) {
		log.debug("finding Proveedor instance by example");
		try {
			List<Proveedor> results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<Proveedor> findByMovil(Object movil) {
		return findByProperty(MOVIL, movil);
	}

	public List<Proveedor> findByNombre(Object nombre) {
		return findByProperty(NOMBRE, nombre);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Proveedor instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Proveedor as model where model." + propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	protected void initDao() {

	}
}